#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define TRUE 1
#define FAILED 0
#define FALSE 0
#define CLEAR_VALUE 0
#define LEN sizeof(struct node)
#define INITVALUE -1


typedef struct node
{
	int data;
	struct node *next;
}Node,*pNode;

int compare(pNode curElem,int subElem){
	if (curElem->data == subElem)
		return TRUE;
	else
		return FALSE;
}
int visit(pNode tmpNode){
	printf("The  %3dth node = %d\n", tmpNode->data);

	return TRUE;
}

pNode initList(){
	printf("Initializing list.\n");
	pNode Head = (pNode)malloc(LEN);
	if(!Head){
		printf("Momory off");
		exit(FAILED);
	}else{
		printf("Initialized.\n");
	}
	return Head;
}

int destroyList(pNode L){//待定
	printf("Destroying list.\n");
	pNode tmpNode1 = L,tmpNode2 = L;
	while(tmpNode1&&tmpNode2){	
		if(tmpNode1){
			tmpNode2 = tmpNode1->next;
			free(tmpNode1);
		}else{
			tmpNode1 = tmpNode2->next;
			free(tmpNode2);
		}
	}
	printf("Destroyed.\n");
	return OK;
}

int clearList(pNode L){
	printf("Clearing list.\n");
	pNode tmpNode = L->next;
	while(tmpNode){
		tmpNode->data = CLEAR_VALUE;
	}
	printf("Cleared.\n");
	return OK;
}

int listEmpty(pNode L){
	printf("Checking list is empty or not.\n");
	pNode tmpNode = L->next;
	while(tmpNode->data){
		tmpNode = tmpNode->next;
	}
	if(tmpNode->next){
		printf("Not empty.\n");
		return FALSE;
	}
	else{
		printf("empty.\n");
		return TRUE;
	}
}

int listLength(pNode L){
	printf("Calculating list length.\n");
	int i = 0;
	pNode tmpNode = L;
	while(tmpNode){
		tmpNode = tmpNode->next;
		i++;
	}
	printf("List length is %d\n",i);
	return i;
}

int getElem(pNode L,int i){
	printf("Get elem value.\n");
	int j = 0,e = INITVALUE;
	pNode tmpNode = L;
	while(tmpNode&&j == i){
		tmpNode = tmpNode->next;
		j++;
	}
	e = tmpNode->data;
	printf("The %dth node value is %d.\n", i,e);
	return e;
}

int locateElem(pNode L,int e){//需修改
	printf("Find the position.\n");
	int i = 0;
	pNode tmpNode = L->next;
	while(!compare(tmpNode,e)){
		tmpNode = tmpNode->next;
		i++;
	}
	if(tmpNode->next == NULL){
		printf("Not found!\n");
		return 0;
	}else{
		printf("The elem located in the %dth node.\n",i);
		return i;
	}
}

int priorElem(pNode L,int cur_e){//need modify
	int pre_e = INITVALUE;
	printf("Searching the pre elem of %d.\n",cur_e);
	pNode tmpNode = L->next;
	while(cur_e != tmpNode->data){
		pre_e = tmpNode->data;
		tmpNode = tmpNode->next;
	}
	if(pre_e == INITVALUE){
		printf("Not found!\n");
		return 0;
	}else{
		printf("The pre elem value = %d.\n",pre_e);
		return pre_e;
	}
}

int nextElem(pNode L,int cur_e){//need modify
	int next_e = INITVALUE;
	printf("Searching the next elem of %d.\n",cur_e);
	pNode tmpNode = L;
	while(cur_e != tmpNode->data){
		tmpNode = tmpNode->next;
	}
	next_e = tmpNode->next->data;
	if(NULL == tmpNode){
		printf("Not found!\n");
		return 0;
	}else{
		printf("The next elem value = %d.\n",next_e);
		return next_e;
	}
}

int listInsert(pNode L,int i,int e){//可优化 MAX_SIZE 可改为MAX_LENGTH
	if(i >= listLength(L)){
		printf("Over length!\n");
		return FALSE;
	}
	int j = 0;
	pNode tmpNode = L;
	while(j != i){
		tmpNode = tmpNode->next;
		j++;
	}
	pNode tmp = tmpNode->next;
	pNode newNode = (pNode)malloc(LEN);
	if(NULL == newNode){
		printf("Memory off!\n");
		exit(FALSE);
	}else{
		newNode->data = e;
		newNode->next = tmp;
		tmpNode->next = newNode;
		printf("Inserted.\n");
	}
	return OK;
}

int listDelete(pNode L,int i){
	int j = 0,e = INITVALUE;
	pNode tmpNode = L,preNode = NULL;
	while(j != i){
		preNode = tmpNode;
		tmpNode = tmpNode->next;
		j++;
	}
	preNode->next = tmpNode->next;
	e = tmpNode->data;
	free(tmpNode);
	printf("Deleted.\n");
	return e;
}

int listTraverse(pNode L){
	pNode tmpNode = L;
	while (tmpNode->next){
		visit(tmpNode);
		tmpNode = tmpNode->next;
	}
	return TRUE;
}

int main(){
	pNode L = initList();
	int next_e = -1, pre_e = -1, cur_e = -1, elem = -1,data = -1,i = 0;
	while (scanf_s("%d", &data)){
		listInsert(L, i, data);
	}
	clearList(L);
	listEmpty(L);
	listLength(L);
	getElem(L, 20);
	locateElem(L, 32);
	priorElem(L, cur_e);
	nextElem(L, cur_e);
	listInsert(L, 8, 80);
	listDelete(L, 5);
	destroyList(L);

	system("pause");
	return OK;
}
